package chat.user.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import chat.user.dao.UserDao;
import chat.user.domain.User;

public class UserService {
	UserDao userDao = new UserDao();

	public void regist(User form) throws UserException
	{
		if(form.getUsername().length()<2 || form.getUsername().length()>8)
		{
			throw new UserException("用户名长度在2-8之间");
		}
		if(form.getPassword().length()<8 || form.getPassword().length()>18)
		{
			throw new UserException("密码长度在8-18之间");
		}
		
		User user = userDao.findByName(form.getUsername());
		if(user!=null)
		{
			throw new UserException("用户名已存在");
		}
		
		//补全信息
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String strDate = df.format(date);
		form.setStatus(2);
		form.setMode(1);
		form.setCreatetime(strDate);
		form.setUpdatetime(strDate);
		form.setUpdateby(form.getUsername());
		form.setLastupdatetime(strDate);
		form.setIsonline(0);
		userDao.addUser(form);
	}

	public User login(User form) throws UserException{
		User user = userDao.findByName(form.getUsername());
		
		if(user==null)
		{
			throw new UserException("用户名不存在");
		}
		if(!form.getPassword().equals(user.getPassword()))
		{
			throw new UserException("密码错误");
		}
//		if(user.getMode()==0)
//		{
//			throw new UserException("用户被冻结");
//		}
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String strDate = df.format(date);
		userDao.updatelast(user,strDate);
		return user;
	}
	
	public void edit(User user,String sb,String value) {
		// TODO Auto-generated method stub
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String strDate = df.format(date);
		user.setUpdatetime(strDate);
		userDao.edit(user,sb,value);
	}

	public User findById(int id) {
		User user = userDao.findById(id);
		return user;
	}

	public List<User> findAll() {
		List<User> userList = userDao.findAll();
		return userList;
	}
	

//	
//	public void updateOnline() {
//		Map<Integer,Integer> map = findMap();
//		Set<Entry<Integer, Integer>> entrySet = map.entrySet(); // key-value的set集合  
//        Iterator<Entry<Integer, Integer>> it2 = entrySet.iterator();  
//        while(it2.hasNext()){  
//             Map.Entry<Integer,Integer> me = (Entry<Integer, Integer>) it2.next();  
//             int k = me.getKey();  
//             int v = me.getValue(); 
//             System.out.println(v);
//            
//        }  
//	}
//	
//	public void findMap() {
//		Date date = new Date();
//		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//		String strDate = df.format(date);
//		List<String> lasttime = userDao.findlasttime();//last时间
//	
//		User user = null;
//		int shicha = 0;
//		for(int i =0;i<lasttime.size();i++)
//		{
//			shicha = userDao.shicha(lasttime.get(i), strDate);
//			user = userDao.findByLastTime(lasttime.get(i));
//			System.out.println(shicha);
//			if(shicha>60)
//            {
//				
//            	userDao.updateOnline(0,user.getId());
//            }
//           
//		}
//		
//		
//	}
//
//	public void updatelasttime(int userId) {
//		Date date = new Date();
//		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//		String strDate = df.format(date);
//		userDao.updatelasttime(userId,strDate);
//	}

	public void updateIsOnline(int id,int isonline) {
		userDao.updateIsOnline(id,isonline);
	}

	public List<User> findOnline() {
		List<User> userList = userDao.findByisOnline();
		return userList;
	}

	public int findNumber() {
		int num = userDao.findNumber();
		return num;
	}
	
}
